package com.baomidou.springwind.excel;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.entity.WorkTime;
import com.baomidou.springwind.entity.NetworkNode;
import com.baomidou.springwind.service.WorkTimeService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * excel文件读取
 *
 * @author ht
 */
@Component
public class WorkTimeDataReader extends ExcelReader<WorkTime> {

    @Autowired
    private WorkTimeService workTimeService;

    @Override
    protected String saveData(List<WorkTime> dataList, Long batchId) {
        EntityWrapper<WorkTime> wrapper = new EntityWrapper<WorkTime>();
        wrapper.eq("batch_id", batchId);
        deleteWorkTimeFile(batchId);
        workTimeService.delete(wrapper);//删除之前导入的数据
        for (WorkTime analysis : dataList) {
            analysis.setBatchId(batchId);
            analysis.setCreateTime(new Date());
            analysis.setOperatorId(getCurrentUserId());
        }
        workTimeService.insertBatch(dataList, 100);
        return "";
    }

    private void deleteWorkTimeFile(Long batchId) {
        System.out.println();
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String path = request.getSession().getServletContext().getRealPath("");
        path = path.substring(0, path.lastIndexOf("\\"));
        path = path.substring(0, path.lastIndexOf("\\"));
        File file = new File(path + "/temp/route/" + batchId + ".obj");
        file.delete();
    }

    @Override
    protected List<WorkTime> verifyData(HSSFWorkbook workbook) {
        List<WorkTime> workTimes = new ArrayList<WorkTime>();
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        WorkTime work;
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            work = new WorkTime();
            work.setName(getStringCellValue(sheet.getRow(i), 0));
            JSONArray josnArray = new JSONArray();
            josnArray.add(getIntCellValue(sheet.getRow(i), 1));
            josnArray.add(getIntCellValue(sheet.getRow(i), 2));
            josnArray.add(getIntCellValue(sheet.getRow(i), 3));
            josnArray.add(getIntCellValue(sheet.getRow(i), 4));
            josnArray.add(getIntCellValue(sheet.getRow(i), 5));
            josnArray.add(getIntCellValue(sheet.getRow(i), 6));
            josnArray.add(getIntCellValue(sheet.getRow(i), 7));
            josnArray.add(getIntCellValue(sheet.getRow(i), 8));
            josnArray.add(getIntCellValue(sheet.getRow(i), 9));
            josnArray.add(getIntCellValue(sheet.getRow(i), 10));
            josnArray.add(getIntCellValue(sheet.getRow(i), 11));
            josnArray.add(getIntCellValue(sheet.getRow(i), 12));
            josnArray.add(getIntCellValue(sheet.getRow(i), 13));
            josnArray.add(getIntCellValue(sheet.getRow(i), 14));
            josnArray.add(getIntCellValue(sheet.getRow(i), 15));
            josnArray.add(getIntCellValue(sheet.getRow(i), 16));
            josnArray.add(getIntCellValue(sheet.getRow(i), 17));
            josnArray.add(getIntCellValue(sheet.getRow(i), 18));
            josnArray.add(getIntCellValue(sheet.getRow(i), 19));
            josnArray.add(getIntCellValue(sheet.getRow(i), 20));
            josnArray.add(getIntCellValue(sheet.getRow(i), 21));
            josnArray.add(getIntCellValue(sheet.getRow(i), 22));
            josnArray.add(getIntCellValue(sheet.getRow(i), 23));
            josnArray.add(getIntCellValue(sheet.getRow(i), 24));
            josnArray.add(getIntCellValue(sheet.getRow(i), 25));
            josnArray.add(getIntCellValue(sheet.getRow(i), 26));
            josnArray.add(getIntCellValue(sheet.getRow(i), 27));
            josnArray.add(getIntCellValue(sheet.getRow(i), 28));
            josnArray.add(getIntCellValue(sheet.getRow(i), 29));
            josnArray.add(getIntCellValue(sheet.getRow(i), 30));
            josnArray.add(getIntCellValue(sheet.getRow(i), 31));
            josnArray.add(getIntCellValue(sheet.getRow(i), 32));
            josnArray.add(getIntCellValue(sheet.getRow(i), 33));
            josnArray.add(getIntCellValue(sheet.getRow(i), 34));
            josnArray.add(getIntCellValue(sheet.getRow(i), 35));
            josnArray.add(getIntCellValue(sheet.getRow(i), 36));
            josnArray.add(getIntCellValue(sheet.getRow(i), 37));
            josnArray.add(getIntCellValue(sheet.getRow(i), 38));
            josnArray.add(getIntCellValue(sheet.getRow(i), 39));
            josnArray.add(getIntCellValue(sheet.getRow(i), 40));
            josnArray.add(getIntCellValue(sheet.getRow(i), 41));
            josnArray.add(getIntCellValue(sheet.getRow(i), 42));
            josnArray.add(getIntCellValue(sheet.getRow(i), 43));
            josnArray.add(getIntCellValue(sheet.getRow(i), 44));
            josnArray.add(getIntCellValue(sheet.getRow(i), 45));
            josnArray.add(getIntCellValue(sheet.getRow(i), 46));
            josnArray.add(getIntCellValue(sheet.getRow(i), 47));
            josnArray.add(getIntCellValue(sheet.getRow(i), 48));
            work.setTime(josnArray.toJSONString());
            workTimes.add(work);
        }
        return workTimes;
    }

    @Override
    protected List<WorkTime> verifyData7(XSSFWorkbook workbook) {
        return null;
    }
}
